package com.sorhis.common.handler;

import com.sorhis.common.exception.SysException;
import com.sorhis.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author xihao.ding
 * @Date 2023/3/17 18:17
 * @Description: TODO
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVO defaultErrorHandler(Exception ex) throws Exception {
        log.error("【后台异常】>>>>>>>>>>>>>> {}" , ex);
        ResultVO<String> resultVO = new ResultVO();
        if(ex instanceof SysException){ //业务逻辑异常
            resultVO.setCode(((SysException) ex).getCode());
            resultVO.setMsg(ex.getMessage());
        }else{
            resultVO.setCode(500);
            resultVO.setMsg("系统异常,请联系管理员");
        }
        return resultVO;
    }


}
